# SHOPPING_CENTER类
# 实例属性
#   商品信息
#   系统说明信息
#   当前用户 -- 多个用户
# 方法
#   登录
#   注册
#   查看商品
#   添加购物车
#   结算

# 用户类
# 实例属性
#   名字
#   密码
#   钱
#   购物车
# 方法
# user class
class User:
    def __init__(self, name, passwd, init_amount):
        self.name = name
        self.passwd = passwd
        self.init_amount = init_amount
        self.shopping_cart = {}


# shoppingsystem
class ShoppingSystem:
    def __init__(self, goods_info, sys_info):
        self.goods_info = goods_info
        self.sys_info = sys_info
        self.userdict = {}

    def register(self, name, passwd, initial_amount):
        # 创建一个新用户，实例化User类
        if name not in self.userdict:
            user = User(name, passwd, initial_amount)
            # 把明子作为key,user对象作为value存在self.userdict里面
            self.userdict[name] = user
        else:
            print("用户已存在")

    # 添加商品
    def shopping(self, goods_id, goods_num):
        shopping_cart = {}
        self.goods_id = goods_id
        self.goods_num = goods_num
        shopping_cart[goods_id] = shopping_cart.get(goods_id, 0) + goods_num
        print(f"{current_system.goods_info[goods_id]['name']}已加入购物车")
        print("-" * 50)
        print("商品编号 商品数量")
        print(f"{goods_id}         {goods_num}")

    # 查看购物车
    def cart_check(self,user):
        print("查看购物车")
        print(f"{'商品编号'} {'商品名'} {'单价'} {'数量'}")
        for i in user.shopping_cart:
            print(f'{i}      {current_system.goods_info[i]["name"]}   {current_system.goods_info[i]["price"]}     {shopping_cart[i]}')

    # 结算
    def cart_count(self):
        pass

    # 退出
    def exit(self):
        print(f"您总共消费了{pay_cart}元")
        print("谢谢惠顾！欢迎您下次光临")
fruit_goods = {
    'F0001': {"name": "苹果", "price": 8},
    'F0002': {"name": "香蕉", "price": 4}
}

medicine_goods = {
    'M0001': {"name": "奥司他韦", "price": 60},
    'M0002': {"name": "抗病毒口服液", "price": 30}
}

system1 = ShoppingSystem(fruit_goods, "水果购物系统")
system2 = ShoppingSystem(medicine_goods, "药品购物系统")

system = [system1, system2]

while 1:
    print("当前平台已有购物系统如下:")
    for k, v in enumerate(system):
        print(f"{k}.{v.sys_info}")
    c1 = input("请输入你的选择(按q退出):")
    if c1 == "q":
        break
    if c1.isdigit() and int(c1) < len(system):
        c1 = int(c1)
        current_system = system[c1]
        print(f'欢迎进入{system[c1].sys_info}'.center(40, "*"))
        while 1:
            c2 = input("1.登录\n2.注册\n请输入你的选择：(按q退出)")
            if c2 == "q":
                break
            if c2 == "1":
                print("请输入账号及密码:")
                zh = input()
                mm = input()
                if zh in current_system.userdict.keys() and current_system.userdict[zh].passwd == mm:
                    print("登录成功！")
                    while 1:
                        print("请输入您的选择:")
                        print("1、查看商品信息")
                        print("2、选择商品并加入购物车")
                        print("3、查看购物车")
                        print("4、结算")
                        print("5、退出")
                        c3 = input("请输入你的选择:(按q退出)")
                        if c3 == "q":
                            break
                        if c3 == "1":
                            print("商品信息如下:")
                            print("商品编号   商品名     单价")
                            for i in current_system.goods_info:
                                print(
                                    f' {i}        {current_system.goods_info[i]["name"]}     {current_system.goods_info[i]["price"]}')
                            print("-" * 50)
                        if c3 == "2":
                            goods_id = input("您想加购的商品编号为:")
                            goods_num = int(input("加购数量为:"))
                            current_system.shopping(goods_id, goods_num)
                        if c3 == "3":
                            current_system.cart_check()
                        if c3 == "4":
                            current_system.cart_count()
                        if c3 == "5":
                            current_system
                else:
                    print("登录失败,请重试。")

            elif c2 == "2":
                name, passwd, amount = input("请输入你的名字，密码，初始金额(用空白分割)").split()
                current_system.register(name, passwd, amount)
                print(f'当前系统有:{current_system.userdict}')
            else:
                print("输入有误！")
    else:
        print("输入有误，请重新输入！")
